<?php
class StudentAttendanceController extends Controller
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout='//layouts/column2';

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		); 
	}
 
	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('index','view','message','error','reportone','reportone1','reportthreeforentireschool','reporttwoforentireschool','reportoneforentireschool','reporttwo','reportthree','pdf_of_reportone','pdf_of_reportthree','pdf_of_reporttwo','pdf'),
				'users'=>array('*'),
			),
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('create','update'),
				'users'=>array('@'),
			),
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('admin','delete'),
				'users'=>array('admin'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}

	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */
	public function actionView($id)
	{
		$this->render('view',array(
			'model'=>$this->loadModel($id),
		));
	}
	
	public function actionPdf()
    {
        //print_r($_REQUEST['data']);
        $html2pdf = Yii::app()->ePdf->HTML2PDF();
        $html2pdf->WriteHTML($this->renderPartial('print',array('data'=>$_REQUEST['data']), true));
             $html2pdf->Output();
	}
	
	public function actionReportone()
	{
		//$this->render('report');
		$model=new StudentAttendance;
		$this->render('reportone',array(
			'model'=>$model,
		));
		//$dataProvider=new CActiveDataProvider('StudentAttendance');
		//$this->render('report',array('dataProvider'=>$dataProvider,));
	}
	
	public function actionReportone1()
	{
		//$this->render('report');
		$model=new StudentAttendance;
		$this->render('reportone1',array(
			'model'=>$model,
		));
		//$dataProvider=new CActiveDataProvider('StudentAttendance');
		//$this->render('report',array('dataProvider'=>$dataProvider,));
	}
	
	
	public function actionPdf_of_reportone()
    {
		//$class_id=$_REQUEST['class_id'];
        $html2pdf = Yii::app()->ePdf->HTML2PDF();
		if(isset($_REQUEST['class_id']))
		{
			$html2pdf->WriteHTML($this->renderPartial('pdf_of_reportone', array('model'=>$this->loadModel($_REQUEST['class_id'],$_REQUEST['from_date'])), true));
			$html2pdf->Output();
		}
		
	}
	
	public function actionReporttwo()
	{
		//$this->render('report');
		$model=new StudentAttendance;
		$this->render('reporttwo',array(
			'model'=>$model,
		));
		//$dataProvider=new CActiveDataProvider('StudentAttendance');
		//$this->render('report',array('dataProvider'=>$dataProvider,));
	}
	
	public function actionPdf_of_reporttwo()
    {
		//$class_id=$_REQUEST['class_id'];
        $html2pdf = Yii::app()->ePdf->HTML2PDF();
		if(isset($_REQUEST['class_id']))
		{
			$html2pdf->WriteHTML($this->renderPartial('pdf_of_reporttwo', array('model'=>$this->loadModel($_REQUEST['class_id'],$_REQUEST['from_date'],$_REQUEST['to_date'],$_REQUEST['no_of_days'])), true));
			 $html2pdf->Output();
		}
		
       
 
        ////////////////////////////////////////////////////////////////////////////////////
	}
	
	public function actionReportthree()
	{
		//$this->render('report');
		$model=new StudentAttendance;
		$this->render('reportthree',array(
			'model'=>$model,
		));
		//$dataProvider=new CActiveDataProvider('StudentAttendance');
		//$this->render('report',array('dataProvider'=>$dataProvider,));
	}
	
	public function actionPdf_of_reportthree()
    {
		//$class_id=$_REQUEST['class_id'];
        $html2pdf = Yii::app()->ePdf->HTML2PDF();
		if(isset($_REQUEST['class_id']))
		{
			$html2pdf->WriteHTML($this->renderPartial('pdf_of_reportthree', array('model'=>$this->loadModel($_REQUEST['class_id'],$_REQUEST['from_date'],$_REQUEST['to_date'],$_REQUEST['no_of_days'])), true));
			 $html2pdf->Output();
		}
		
       
 
        ////////////////////////////////////////////////////////////////////////////////////
	}
	
	public function actionReportoneforentireschool()
	{
		$html2pdf = Yii::app()->ePdf->HTML2PDF();
		
		if(isset($_REQUEST['classid']))
		{
			$html2pdf->WriteHTML($this->renderPartial('reportoneforentireschool', array('model'=>$this->loadModel($_REQUEST['classid'],$_REQUEST['from_date'])), true));
			$html2pdf->Output();
		}
		
	}
	
	public function actionReporttwoforentireschool()
	{
		$html2pdf = Yii::app()->ePdf->HTML2PDF();
		
		if(isset($_REQUEST['classid']))
		{
			$html2pdf->WriteHTML($this->renderPartial('reporttwoforentireschool', array('model'=>$this->loadModel($_REQUEST['classid'],$_REQUEST['from_date'],$_REQUEST['to_date'],$_REQUEST['no_of_days'])), true));
			$html2pdf->Output();
		}
		
	}
	
	public function actionReportthreeforentireschool()
	{
		$html2pdf = Yii::app()->ePdf->HTML2PDF();
		
		if(isset($_REQUEST['classid']))
		{
			$html2pdf->WriteHTML($this->renderPartial('reportthreeforentireschool', array('model'=>$this->loadModel($_REQUEST['classid'],$_REQUEST['from_date'],$_REQUEST['to_date'],$_REQUEST['no_of_days'])), true));
			$html2pdf->Output();
		}
		
	}
	
	
	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 */
	 
	public function actionCreate()
	{
		$model=new StudentAttendance;
		if(isset($_POST['StudentAttendance']))
		{
			$model->attributes=$_POST['StudentAttendance'];
			$model->student_id = $_POST['StudentAttendance']['student_id'];
			$model->class_id= $_POST['StudentAttendance']['class_id'];
			$model->attendance_date = $_POST['StudentAttendance']['attendance_date'];
			$attendancedate=$model->attendance_date;
			$classid=$model->class_id;
			$keyword= explode(",", $model->student_id);
			//$keyword_value = array_shift($keyword);
			if(reset($keyword)==0)
			{
				$model->save();
				$this->redirect(array('message','all_present'=>'all_present'));
			}
			for ($j=0; $j<=sizeof($keyword)-1; $j++)
			{
				
				//$query = $model->get_student_attendance($keyword[$j],$classid);
				$query=Registration::model()->findByAttributes(array('registration_no'=>$keyword[$j],'class'=>$classid));
				if (empty($query))
				{
					
					//throw new CHttpException(501,'Absentee Student Numbers already Inserted .');
					//$model->save(); 
					//return false;
					$this->redirect(array('message','wrogn_number'=>'wrogn_number'));
				}
			}
			if ( count($keyword) > count(array_unique($keyword)) ) 
			{
				$this->redirect(array('message','error'=>'error'));
			}
			else
			{
				//$sql = $model->get_student_attendance($classid,$attendancedate);
				//$sql = StudentAttendance::model()->findAllBySql('(SELECT * FROM student_attendance WHERE class_id='.$classid.' and attendance_date="'.$attendancedate.'")');
				
				//$sql = StudentAttendance::model()->findAll(array("condition"=>"attendance_date='.$attendancedate.'"  AND "class_id='.$classid.'" 'params'=>array(':status'=>1, ':date'=>$date),));
				//$sql=StudentAttendance::model()->findAll(array( 'condition'=>'attendance_date=:attendance_date AND class_id >=:classid', 'params'=>array(':attendance_date'=>$attendancedate, ':classid'=>$classid),));
				$sql=StudentAttendance::model()->findByAttributes(array('attendance_date'=>$attendancedate,'class_id'=>$classid));
				if (empty($sql))
				{
					
					
					for ($i=0; $i<=sizeof($keyword)-1; $i++)
					{
						
						$model->id ="";
						$model->student_id = $keyword[$i];
						$model->isNewRecord = true;
						$model->save(); 
						
					}
					if($model->save())
					{
						$this->redirect(array('message'));
					}	
					
				}
				else
				{
					$this->redirect(array('message','inserted'=>'inserted'));
				}
			}
			
		}

		$this->render('create',array(
			'model'=>$model,
		));
	
	}
	
	
	public function actionCreate1()
	{
		$model=new StudentAttendance;

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['StudentAttendance']))
		{
			$model->attributes=$_POST['StudentAttendance'];
			$model->student_id = $_POST['StudentAttendance']['student_id'];
			$model->class_id= $_POST['StudentAttendance']['class_id'];
			$model->attendance_date = $_POST['StudentAttendance']['attendance_date'];
			$attendancedate=$model->attendance_date;
			
			$classid=$model->class_id;
			$keyword= explode(",", $model->student_id);
			if ( count($keyword) > count(array_unique($keyword)) ) 
			{
				$message="You have Entered Duplicate Student Number Try Again";
				//throw new CHttpException('',$message);
				$this->redirect(array('message','error'=>'error'));
				
			}
			else
			{
				//$get_class = $model->student_attendance($classid,$attendancedate);
				$sql = StudentAttendance::model()->findAllBySql('(SELECT * FROM student_attendance WHERE class_id='.$classid.' and attendance_date="'.$attendancedate.'")');
				if (empty($sql))
				{
					for ($j=0; $j<=sizeof($keyword)-1; $j++)
					{
						$query=StudentAttendance::model()->findAllBySql('(SELECT * FROM registration WHERE registration_no='.$keyword[$j].' and class="'.$classid.'")');
						if($error=Yii::app()->errorHandler->error)
						if ( CDbException == $error->type)
						{
							$message="You have Entered wrong student number please Try Again";
							$this->redirect(array('message','error'=>'error'));
						}
						if (empty($query))
						{
							
							$message="You have Entered wrong student number please Try Again";
							$this->redirect(array('message','wrogn_number'=>'wrogn_number'));
							//throw new CHttpException(501,'Enter wrong student number .');
						}
					}
					for ($i=0; $i<=sizeof($keyword)-1; $i++)
					{
					
					
							$model->id ="";
							$model->student_id = $keyword[$i];
							$model->isNewRecord = true;
							$model->save(); 
						
					}
					if($model->save())
					{
						$this->redirect(array('message'));
					}	
				} 
				else
				{
					$message="Absentee Student Numbers already Inserted";
					$this->redirect(array('message','inserted'=>'inserted'));
					//throw new CHttpException(501,'Absentee Student Numbers already Inserted .');
				}
			}
			
		}

		$this->render('create',array(
			'model'=>$model,
		));
	}
	public function save($runValidation=true,$attributes=null)
	{
   		 if(!$runValidation || $this->validate($attributes))
       	 return $this->getIsNewRecord() ? $this->insert($attributes) : $this->update($attributes);
    	else
        	return false;
	}
	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionUpdate($id)
	{
		$model=$this->loadModel($id);

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['StudentAttendance']))
		{
			$model->attributes=$_POST['StudentAttendance'];
			if($model->save())
				$this->redirect(array('view','id'=>$model->id));
		}

		$this->render('update',array(
			'model'=>$model,
		));
	}

	/**
	 * Deletes a particular model.
	 * If deletion is successful, the browser will be redirected to the 'admin' page.
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDelete($id)
	{
		if(Yii::app()->request->isPostRequest)
		{
			// we only allow deletion via POST request
			$this->loadModel($id)->delete();

			// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
			if(!isset($_GET['ajax']))
				$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
		}
		else
			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
	}

	/**
	 * Lists all models.
	 */
	public function actionIndex()
	{
		$dataProvider=new CActiveDataProvider('StudentAttendance');
		$this->render('index',array(
			'dataProvider'=>$dataProvider,
		));
	}
	public function actionMessage()
	{
		$this->render('message');
		
	}

	/**
	 * Manages all models.
	 */
	public function actionAdmin()
	{
		$model=new StudentAttendance('search');
		$model->unsetAttributes();  // clear any default values
		if(isset($_GET['StudentAttendance']))
			$model->attributes=$_GET['StudentAttendance'];

		$this->render('admin',array(
			'model'=>$model,
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=StudentAttendance::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='student-attendance-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
	
	public function actionError() 
	{
		
	}
	
}
